package com.sanjiahantang.ythshopping.presenter;

import android.content.Context;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.config.StaticVelue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.ExceptionHelper;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.sanjiahantang.ythshopping.model.DillBean;
import com.sanjiahantang.ythshopping.model.SubmitOrderModel;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.CreateOrderView;
import com.squareup.okhttp.Request;

import java.util.Map;

/**
 * Created by sks on 2015/10/12.
 */
public class SubmitOrderPresenter extends BasePresenter {
    private CreateOrderView createOrderView;
    public SubmitOrderPresenter(CreateOrderView createOrderView){
        this.createOrderView = createOrderView;
    }

    public void requestdill(final Context context,String surplus,String address_id,String pay_id,
                        String integral,String bond,String pay_ship,String message,
                        String hoss,String money_paid,String pwd,DillBean bean,boolean oversea,String overstr){
        Map<String, String> params = getDefaultMD5Params("order", "create");
        params.put("key", ConfigValue.DATA_KEY);
        params.put("carts",getCarParam());
        params.put("address_id",address_id);
        params.put("surplus",surplus);
        params.put("pay_id",pay_id);
        params.put("integral",integral);
        params.put("bond",bond);
        params.put("pay_ship",pay_ship);
        params.put("message",message);
        params.put("hoss",hoss);
        params.put("money_paid",money_paid);
        if (!pwd.equals(""))
        {
            params.put("pwd",pwd);
        }
        params.put("inv_type",bean.getInv_type());
        params.put("inv_content",bean.getInv_content());
        if (bean.getInv_type().equals("normal_invoice"))
        {
            params.put("inv_payee_type",bean.getInv_payee_type());
            if (bean.getInv_payee_type().equals("unit_inv"))
            {
                params.put("inv_payee",bean.getInv_payee());
            }
        }else {
            params.put("vat_inv_company_name",bean.getVat_inv_company_name());
            params.put("vat_inv_taxpayer_id",bean.getVat_inv_taxpayer_id());
            params.put("vat_inv_registration_address",bean.getVat_inv_registration_address());
            params.put("vat_inv_registration_phone",bean.getVat_inv_registration_phone());
            params.put("vat_inv_deposit_bank",bean.getVat_inv_deposit_bank());
            params.put("vat_inv_bank_account",bean.getVat_inv_bank_account());
            params.put("inv_consignee_name",bean.getInv_consignee_name());
            params.put("inv_consignee_phone",bean.getInv_consignee_phone());
            params.put("inv_consignee_province",bean.getInv_consignee_province());
            params.put("inv_consignee_city",bean.getInv_consignee_city());
            params.put("inv_consignee_district",bean.getInv_consignee_district());
            params.put("inv_consignee_address",bean.getInv_consignee_address());
        }

        if (oversea)
        {
            params.put("supplier_pay",overstr);
        }

        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "order/create",
                params, new BaseDelegate.ResultCallback<SubmitOrderModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(SubmitOrderModel response, Object tag) {
                        createOrderView.result(response);
                    }
                },true);
    }

    public void request(final Context context,String surplus,String address_id,String pay_id,
                        String integral,String bond,String pay_ship,String message,
                        String hoss,String money_paid,String pwd,boolean oversea,String overstr){
        Map<String, String> params = getDefaultMD5Params("order", "create");
        params.put("key", ConfigValue.DATA_KEY);
        params.put("carts",getCarParam());
        params.put("address_id",address_id);
        params.put("surplus",surplus);
        params.put("pay_id",pay_id);
        params.put("integral",integral);
        params.put("bond",bond);
        params.put("pay_ship",pay_ship);
        params.put("message",message);
        params.put("hoss",hoss);
        params.put("money_paid",money_paid);
        if (!pwd.equals(""))
        {
            params.put("pwd",pwd);
        }

        if (oversea)
        {
            params.put("supplier_pay",overstr);
        }
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "order/create",
                params, new BaseDelegate.ResultCallback<SubmitOrderModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(SubmitOrderModel response, Object tag) {
                        createOrderView.result(response);
                    }
                },true);
    }

    public void requestInfogoods(final Context context,String surplus,String cardid,String address_id,String pay_id,
                        String integral,String bond,String pay_ship,String message,
                        String hoss,String money_paid,String pwd,boolean oversea,String overstr){
        Map<String, String> params = getDefaultMD5Params("order", "create");
        params.put("key", ConfigValue.DATA_KEY);
        params.put("carts",cardid);
        params.put("address_id",address_id);
        params.put("surplus",surplus);
        params.put("pay_id",pay_id);
        params.put("integral",integral);
        params.put("bond",bond);
        params.put("pay_ship",pay_ship);
        params.put("message",message);
        params.put("hoss",hoss);
        params.put("money_paid",money_paid);
        if (!pwd.equals(""))
        {
            params.put("pwd",pwd);
        }
        if (oversea)
        {
            params.put("supplier_pay",overstr);
        }

        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "order/create",
                params, new BaseDelegate.ResultCallback<SubmitOrderModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(SubmitOrderModel response, Object tag) {
                        createOrderView.result(response);
                    }
                },true);
    }

    public void requestInfogoodsDill(final Context context, String surplus, String cardid, String address_id, String pay_id,
                                     String integral,String bond, String pay_ship, String message,
                                     String hoss, String money_paid, String pwd, DillBean bean,boolean oversea,String overstr){
        Map<String, String> params = getDefaultMD5Params("order", "create");
        params.put("key", ConfigValue.DATA_KEY);
        params.put("carts",cardid);
        params.put("address_id",address_id);
        params.put("surplus",surplus);
        params.put("pay_id",pay_id);
        params.put("integral",integral);
        params.put("bond",bond);
        params.put("pay_ship",pay_ship);
        params.put("message",message);
        params.put("hoss",hoss);
        params.put("money_paid",money_paid);
        if (!pwd.equals(""))
        {
            params.put("pwd",pwd);
        }
        params.put("inv_type",bean.getInv_type());
        params.put("inv_content",bean.getInv_content());
        if (bean.getInv_type().equals("normal_invoice"))
        {
            params.put("inv_payee_type",bean.getInv_payee_type());
            if (bean.getInv_payee_type().equals("unit_inv"))
            {
                params.put("inv_payee",bean.getInv_payee());
            }
        }else {
            params.put("vat_inv_company_name",bean.getVat_inv_company_name());
            params.put("vat_inv_taxpayer_id",bean.getVat_inv_taxpayer_id());
            params.put("vat_inv_registration_address",bean.getVat_inv_registration_address());
            params.put("vat_inv_registration_phone",bean.getVat_inv_registration_phone());
            params.put("vat_inv_deposit_bank",bean.getVat_inv_deposit_bank());
            params.put("vat_inv_bank_account",bean.getVat_inv_bank_account());
            params.put("inv_consignee_name",bean.getInv_consignee_name());
            params.put("inv_consignee_phone",bean.getInv_consignee_phone());
            params.put("inv_consignee_province",bean.getInv_consignee_province());
            params.put("inv_consignee_city",bean.getInv_consignee_city());
            params.put("inv_consignee_district",bean.getInv_consignee_district());
            params.put("inv_consignee_address",bean.getInv_consignee_address());
        }
        if (oversea)
        {
            params.put("supplier_pay",overstr);
        }
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "order/create",
                params, new BaseDelegate.ResultCallback<SubmitOrderModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(SubmitOrderModel response, Object tag) {
                        createOrderView.result(response);
                    }
                },true);
    }
    public String getCarParam()
    {
        String Carstr="";
        for (int i = 0; i< StaticVelue.selectGoodsList.size(); i++)
        {
            if (i!=StaticVelue.selectGoodsList.size()-1)
            {
                Carstr=Carstr+StaticVelue.selectGoodsList.get(i).getRec_id()+",";
            }else {
                Carstr=Carstr+StaticVelue.selectGoodsList.get(i).getRec_id();
            }
        }
        return Carstr;
    }
}
